package com.kingsoft.dc.khaos.module.spark.util;



import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class DateTimeForamtUtil {
    private static Map<String, SimpleDateFormat> datetimeFormatMap = new HashMap();
    private static void setDateFormat(String ksyunFormat,String nomalFormat){
        SimpleDateFormat df = new SimpleDateFormat(nomalFormat);
        df.setLenient(false);
        datetimeFormatMap.put(ksyunFormat,df);
    }
    static{
        setDateFormat("YYYY-MM-DD","yyyy-MM-dd");
        setDateFormat( "YYYYMMDD", "yyyyMMdd" );
        setDateFormat( "YYYY" ,"YYYY" );
        setDateFormat( "YYYYMM" , "yyyyMM");
        setDateFormat( "YYYY-MM" ,"yyyy-MM" );
        setDateFormat( "MM-DD" , "MM-dd");
        setDateFormat(  "MMDD", "MMdd");
        setDateFormat( "MM" ,"MM" );
        setDateFormat( "DD" ,"dd" );
        setDateFormat( "HH:MM:SS" ,"HH:mm:ss" );
        setDateFormat( "HHMMSS" ,"HHmmss" );
        setDateFormat(  "HH:MM:SS:NNN","HH:mm:ss:SSS" );
        setDateFormat( "HHMMSSNNN" ,"HHmmssSSS" );
        setDateFormat( "YYYY-MM-DDTHH:MM:SS" , "yyyy-MM-dd HH:mm:ss");
        setDateFormat( "YYYYMMDDHHMMSS" ,"yyyyMMddHHmmss" );
        setDateFormat(  "MMDDHHMMSS","MMddHHmmss" );
        setDateFormat( "YYYYMMDDHHMMSSNNN" , "yyyyMMddHHmmssSSS");
        setDateFormat( "YYYY-MM-DDTHH:MM:SS:NNN" ,"yyyy-MM-dd HH:mm:ss:SSS" );
    }

    public static DateFormat getFormater(String formater){
        return datetimeFormatMap.get(formater);
    }

    public static boolean checkFormaterExist(String formater){
        return datetimeFormatMap.get(formater) != null;
    }

    public static Date formatDatetime(String formater, String value) throws ParseException {
        return datetimeFormatMap.get(formater).parse(value);
    }
}
